package com.wo.android.pockybill.adapter;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Paint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.wo.android.pockybill.R;
import com.wow.wa.bill.serivce.BillService;
import com.wow.wa.bill.serivce.BillServiceImpl;
import com.wow.wa.bill.serivce.CommonComent;
import com.wow.wa.bill.serivce.CommonComentImpl;
import com.wow.wa.bill.util.Args;
import com.wow.wa.bill.vo.Bill;
import com.wow.wa.bill.vo.Card;
import com.wow.wa.bill.vo.Category;

public class BillAdapter extends BaseAdapter {

	private List<Bill> bills = null;
	private Context ct = null;
	private CommonComent common;
	private BillService service;
	private Map<String, String> map = new HashMap<String, String>();
	private Card card;
	private boolean isBtnShowed = false;

	public BillAdapter(Context context, List<Bill> bills, final Card card) {
		this.service = new BillServiceImpl(context);
		this.card = card;
		this.bills = bills;
		this.ct = context;
		common = new CommonComentImpl(context);

		if (card.get_id() == 0) {
			List<Category> categorys = this.service.getCategorys(card.get_id()
					+ "", this.ct);

			for (Category category : categorys) {
				map.put(category.get_id().toString(), category.getName());
			}
		} else {
			List<Category> categorys = this.service.getCategorys(this.ct);

			for (Category category : categorys) {
				map.put(category.get_id().toString(), category.getName());
			}
		}

	}

	public BillAdapter(Context context, List<Bill> bills, final Card card,
			boolean showBtn) {
		this(context, bills, card);
		this.isBtnShowed = showBtn;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return bills.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return bills.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@SuppressLint("ResourceAsColor")
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		View view = LayoutInflater.from(this.ct).inflate(
				R.layout.bill_plus_view, null);
		TextView name = (TextView) view.findViewById(R.id.name);
		TextView dollar = (TextView) view.findViewById(R.id.dollar);
		if (isBtnShowed) {
			view.findViewById(R.id.payBtn).setVisibility(View.VISIBLE);
		} else {
			view.findViewById(R.id.payBtn).setVisibility(View.GONE);
		}

		TextView dueDate = (TextView) view.findViewById(R.id.dueDate);
		TextView category = (TextView) view.findViewById(R.id.category);
		// TextView duteDateText = (TextView)
		// view.findViewById(R.id.duteDateText);
		// TextView stateText = (TextView) view.findViewById(R.id.stateText);

		final Bill bill = this.getBills().get(position);

		// stateView.setBackgroundColor(greenColor);

		// stateView.setBackgroundColor(Color.GREEN);

		dollar.setText(common.stringtoMoney(bill.getDollar()));
		name.setText(bill.getName());
		dueDate.setText(bill.getDueDate());
		if (StringUtils.isBlank(bill.getDollar())) {
			dollar.setText("0");
		}

		if (StringUtils.equals(bill.getIsPayed(), Args.Common.STRING_Y)) {
			this.deletLine(name);
			this.deletLine(dollar);
		}

		return view;
	}

	private void deletLine(TextView texview) {
		Paint paint = texview.getPaint();
		paint.setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
		paint.setAntiAlias(true);
	}

	public List<Bill> getBills() {
		return bills;
	}

	public void setBills(List<Bill> bills) {
		this.bills = bills;
	}

}
